package com.wg.msshopsp.backendmanagement.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wg.msshopsp.backendmanagement.user.entity.User;
import com.wg.msshopsp.backendmanagement.user.service.UserService;
import com.wg.msshopsp.entity.QueryPageDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Tag(name = "UserController", description = "用户管理控制器")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/shop/userMS")
    @Operation(summary = "新增用户")
    public int insertUser(@RequestBody User user) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(sdf.format(new Date()));
        return userService.save(user)? 1 : 0;
    }

    @DeleteMapping("/shop/userMS/{id}")
    @Operation(summary = "删除用户")
    public int deleteUser(@PathVariable Integer id) {
        return userService.removeById(id)? 1 : 0;
    }

    @PutMapping("/shop/userMS")
    @Operation(summary = "修改用户")
    public int updateUser(@RequestBody User user) {
        return userService.updateById(user)? 1 : 0;
    }

    @GetMapping("/shop/userMS")
    @Operation(summary = "模糊查询用户")
    public Page<User> queryUsers(User user, QueryPageDto queryPageDto)
    {
        return userService.queryUsers(user, queryPageDto);
    }
}
